package views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import common.Contexto;

import controllers.CuentasController;
import controllers.MainController;

@SuppressWarnings("serial")
public class MainPanel extends JPanel {
	private JToolBar toolbar;
	private JButton socioB, mostrarSociosB, reservaCanchaB, reservaClaseB, imputarCuotasB, inicializarDemoB;
	
	private MainController mainController;
	private CuentasController cuentasController;
	private ClasePanel clasePanel;
	private SocioTablePanel socioTablePanel;
	
	public static void main(String[] args) {
		MainPanel mainPanel = new MainPanel();
		mainPanel.showPanel();
	}
	
	public MainPanel() {
		mainController = new MainController();
		cuentasController = new CuentasController();
		initGui();
		layoutComponents();
		initActions();
	}
	
	private void initGui() {
		clasePanel = new ClasePanel();
		socioTablePanel = new SocioTablePanel();
		socioB = new JButton();
		mostrarSociosB = new JButton();
		reservaCanchaB = new JButton();
		reservaClaseB = new JButton();
		imputarCuotasB = new JButton();
		inicializarDemoB = new JButton();
	}

	private void layoutComponents() {
		this.setLayout(new BorderLayout());
        
        setPreferredSize(new Dimension(450, 130));
        toolbar = new JToolBar();
        add(toolbar, BorderLayout.PAGE_START);
        
        toolbar.add(socioB);
        toolbar.add(mostrarSociosB);
        toolbar.add(reservaCanchaB);
        toolbar.add(reservaClaseB);
        toolbar.add(imputarCuotasB);
        toolbar.add(inicializarDemoB);
	}
	
	private void initActions() {
		socioB.setAction(new AbstractAction("Registrar Socio"){
			@Override
			public void actionPerformed(ActionEvent e) {
				mainController.showSocioPanel();
			}			
		});
		
		mostrarSociosB.setAction(new AbstractAction("Consultar Socios"){
			@Override
			public void actionPerformed(ActionEvent e) {
				socioTablePanel.showPanel();
			}			
		});
		
		reservaCanchaB.setAction(new AbstractAction("Reservar Cancha"){
			@Override
			public void actionPerformed(ActionEvent e) {
				mainController.showReservaPanel();
			}			
		});
		
		reservaClaseB.setAction(new AbstractAction("Reservar Clase"){
			@Override
			public void actionPerformed(ActionEvent e) {
				clasePanel.showPanel();
			}			
		});
		
		imputarCuotasB.setAction(new AbstractAction("Imputar Cuotas"){
			@Override
			public void actionPerformed(ActionEvent e) {
				cuentasController.imputarCuotasSocios();
				Contexto.displayFeedbackMessage(MainPanel.this, "Las cuotas fueron imputadas con exito");
			}			
		});
		
		inicializarDemoB.setAction(new AbstractAction("Inicializar Contexto Demo"){
			@Override
			public void actionPerformed(ActionEvent e) {
				Contexto.inicializarContextoDemo();
				Contexto.displayFeedbackMessage(MainPanel.this, "El contexto de demo se ha inicializado con exito");
			}			
		});
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
        mainFrame.setTitle("Berazategui Tenis Club");
        mainFrame.getContentPane().add(this);
        mainFrame.setPreferredSize(new Dimension(710,340));
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
	}
}
